package com.keeping.aiimagery.Config;

import com.unfbx.chatgpt.OpenAiStreamClient;
import com.unfbx.chatgpt.interceptor.OpenAILogger;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * OpenAI配置类
 * Created By Ranger on 2023/3/28.
 */
@Configuration
public class OpenAiStreamConfig {

    static String apiKey = "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";
    

    @Bean
    public OpenAiStreamClient openAiStreamClient() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                //.proxy(proxy)//自定义代理
                .addInterceptor(httpLoggingInterceptor)//自定义日志
                .connectTimeout(30, TimeUnit.MINUTES)//自定义超时时间
                .writeTimeout(30, TimeUnit.MINUTES)//自定义超时时间
                .readTimeout(30, TimeUnit.MINUTES)//自定义超时时间
                .build();
        List<String> objects = new ArrayList<>();
        objects.add(apiKey);
        return OpenAiStreamClient.builder()
                //.apiHost()
                .apiKey(objects)
                //.keyStrategy(new FirstStrategy())
                .okHttpClient(okHttpClient).build();
    }
}
